package execl.test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class addColumnToExecl {
	public void add(String path1 , String path2 ,
			int[] column1 , int[] column2,
			int judgment1 , int judgement2
			) throws IOException, BiffException, RowsExceededException, WriteException{
		File path2File = new File(path2);
		Workbook path2Workbook = Workbook.getWorkbook(path2File);
		Sheet path2Sheet = path2Workbook.getSheet(0);
		Map<String , String[]> mapPath2 = new HashMap<String , String[]>(); 
		String[] conditions = null;
		for(int i = 0 ; i < path2Sheet.getRows() ; i++){
			conditions = new String[column2.length];
			for(int j = 0 ; j < path2Sheet.getColumns() ; j++){
				for(int s = 0 ; s < column2.length ; s++){
					if(j == column2[s]){
						conditions[s] = path2Sheet.getCell(j,i).getContents().trim();
					}
				}
			}
			mapPath2.put(path2Sheet.getCell(judgement2, i).getContents().trim(), conditions);
		}
		
		File path1File = new File(path1);
		Workbook rwb = Workbook.getWorkbook(path1File);
		WritableWorkbook path1Workbook = Workbook.createWorkbook(path1File);
		WritableSheet path1Sheet = path1Workbook.getSheet(0);
		for(int i = 0 ; i  < path1Sheet.getRows() ; i++){
			String[] content = mapPath2.get(path1Sheet.getCell(judgment1,i));
			if(content != null){
				for(int j = 0 ; j <  column1.length ; j++){
					Label label = new Label(column1[j],i,content[j]);
					path1Sheet.addCell(label);
				}
			}
		}
		path1Workbook.write();
		path1Workbook.close();
		
	}
}
